/** 
 * 用户 Store
 * @author LiQingSong
 */
import { login, logout, getInfo, getLoginState } from '@/service/user';
import { getImg } from '@/service/app';
import { getToken, setToken, removeToken } from '@/service/lib/localToken';
import { resetRouter } from '@/router';
import { isExternal } from '@/utlis/validate';
import { serverLoginUrl, serverLogoutUrl, siteLoginRouter } from '@/settings';
import hiSetting from '@/hiSetting'
const state = {
  token: getToken(),
  name: '',
  avatar: '',
  msgtotal: 0,
  roles: [],
  userInfo: {},
  imgurl: ""
};
const mutations = {
  SET_TOKEN: (state, token) => {
    state.token = token;
  },
  SET_NAME: (state, name) => {
    state.name = name;
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar;
  },
  SET_MSGTOTAL: (state, msgtotal) => {
    state.msgtotal = msgtotal;
  },
  SET_ROLES: (state, roles) => {
    state.roles = roles;
  },
  SET_USERINFO: (state, userInfo) => {
    state.userInfo = userInfo
  },
  SET_IMGURL: (state, imgurl) => {
    state.imgurl = imgurl
  }
};
const actions = {
  // 用户登录
  login({ commit }, userInfo) {
    const { username, password } = userInfo;
    return new Promise((resolve, reject) => {
      login(serverLoginUrl, { username: username, password: password }).then(response => {
        const { data } = response;
        const { token } = data;
        commit('SET_TOKEN', token);
        setToken(token);
        resolve(data);
      }).catch(error => {
        reject(error);
      });
    });
  },

  // 获取用户登录状态
  getLoginState({ commit }) {
    return new Promise((resolve, reject) => {
      getLoginState(state.token).then(response => {
        const { data } = response;
        commit('SET_NAME', data.accountid);
        resolve(data);
      }).catch(error => {
        reject(error.msg || 'Error');
        console.log(error);
      });
    });
  },
  setUserInfo({ commit, state }, userInfo) {
    commit('SET_USERINFO', userInfo)
  },
  // 获取用户信息
  getInfo({ commit, state }) {
    commit('SET_TOKEN', "admin")
    return new Promise((resolve, reject) => {
      getInfo(state.token).then(response => {
        const { data } = response;
        if (data) {
          var userInfo = {
            userCode: data.userCode,  //用户编码
            nickName: data.nickName,  //昵称
            workUnit: data.workUnit,  //工作单位
            duties: data.duties, //职能
            address: data.address,   //地址
            briefIntroduction: data.briefIntroduction,//简介
            headimgurl: data.headimgurl,//头像地址
          }

          // commit('SET_NAME', name);
          commit('SET_USERINFO', userInfo)
        }

        var user = {
          avatar: "",
          msgtotal: 10,
          roles: ["admin"]
        }
        const { roles, avatar, msgtotal } = user;

        // roles must be a non-empty array
        if (!roles || roles.length <= 0) {
          reject('用户没有角色权限!');
        }

        commit('SET_ROLES', roles);
        commit('SET_AVATAR', avatar);
        commit('SET_MSGTOTAL', msgtotal);

        resolve(user);
      }).catch(error => {
        reject(error.msg || 'Error');
        // console.log(error);
      });
    });
  },

  // 用户退出
  logout({ commit }) {
    window.location.href = hiSetting.server + "/logout";
    return
    if (isExternal(serverLogoutUrl)) {
      return new Promise((resolve) => {
        commit('SET_TOKEN', '');
        commit('SET_ROLES', []);
        window.location.href = serverLogoutUrl;
        resolve({ isExternal: true, siteLoginRouter: siteLoginRouter });
      });
    } else {
      return new Promise((resolve, reject) => {
        logout(serverLogoutUrl).then(() => {
          commit('SET_TOKEN', '');
          commit('SET_ROLES', []);
          removeToken();
          resetRouter();
          resolve({ isExternal: false, siteLoginRouter: siteLoginRouter });
        }).catch(error => {
          reject(error);
        });
      });
    }
  },
  // 删除Token
  resetToken({ commit }) {
    return new Promise(resolve => {
      commit('SET_TOKEN', '');
      commit('SET_ROLES', []);
      removeToken();
      resolve();
    });
  }


};

export default {
  namespaced: true,
  state,
  mutations,
  actions
};